package sisdroid.framework;

import java.util.HashSet;
import java.util.Set;
import sisdroid.framework.rest.*;
import sisdroid.framework.sensors.Sensor;
import sisdroid.framework.model.*;
import sisdroid.framework.observers.ScanCompletedObserver;

public class SisDroidImpl implements SisDroid
{
	
	Sensor 								_discoverer;
	SisRestClient							_client;
	ScanCompletedObserver					_scanCompletedObserver;
	
	
	
	
	public SisDroidImpl( SisFactory fac )
	{
		if ( fac == null )
		{
			throw new NullPointerException( "Factory cannot be null, shame one you! " );
		}
		_discoverer					= fac.createDiscoverers();
		_client						= new SisRestClient( fac ); // TODO: push to the factory
		_scanCompletedObserver 		= new ScanCompletedObserver() {
			
			@Override
			public boolean scanCompleted(Set<Net> data)
			{
				return true;
			}
		};
	}
	
	public Set<InfoPackage> Search(Set<Net> locations, Set<String> filter )
	{
		return null;
		
	}
	
	public void setObserver( ScanCompletedObserver observer )
	{
		_scanCompletedObserver = observer;
	}

 	@Override
	public Set<InfoPackage> Search(Set<String> filter)
	{
 		Set<Net> nets = _discoverer.getNetIDS();
 		
 		if ( _scanCompletedObserver.scanCompleted( nets ) )
 		{
 			return _client.Search( nets, filter );	
 		}
 		return new HashSet<InfoPackage>();
	}
 	
 	@Override
 	public boolean hasActiveDiscovere( )
 	{
 		return ( this._discoverer != null );
 	}


	
	public void Store(InfoPackage ip, Set<Net> loc )
	{
		_client.Store( ip, loc );
	}
 	
	@Override
	public void Store(InfoPackage ip )
	{
		this.Store(ip,this._discoverer.getNetIDS( ) );
	}
}
